package cn.xttc.edu.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 杨磊
 * 登录拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        if ("/".equals(uri) || "/loginPage".equals(uri) || "/login".equals(uri) || uri.contains("/wx") || "/checkCode".equals(uri)) {
            return true;
        }

        //获取登录用户的session
        Object loginAdmin = request.getSession().getAttribute("loginAdmin");

        //session不存在，即用户尚未登录
        if (loginAdmin == null) {
            request.getSession().setAttribute("msg", "请先登录");
            //返回首页
            response.sendRedirect("/loginPage");
            return false;
        } else {
            return true;
        }
    }
}
